#!/bin/sh
#
# Start adb daemon service....
#

case "$1" in
  start)

	export service_adb_tcp_port=5555
	echo "Configuring ADB USB gadget driver..."
	UDC=`ls /sys/class/udc/| awk '{print $1}'`
	mkdir /dev/usb-ffs -m 0770
	mkdir /dev/usb-ffs/adb -m 0770
	mount -t configfs none /sys/kernel/config
	mkdir /sys/kernel/config/usb_gadget/g1  -m 0770
	sleep 1
	echo 0x2207 > /sys/kernel/config/usb_gadget/g1/idVendor
	echo 0x0006 > /sys/kernel/config/usb_gadget/g1/idProduct
	mkdir /sys/kernel/config/usb_gadget/g1/strings/0x409   -m 0770
	sleep 1
	echo "0123456789ABCDEF" > /sys/kernel/config/usb_gadget/g1/strings/0x409/serialnumber
	echo "rockchip"  > /sys/kernel/config/usb_gadget/g1/strings/0x409/manufacturer
	echo "rk3xxx"  > /sys/kernel/config/usb_gadget/g1/strings/0x409/product

	mkdir /sys/kernel/config/usb_gadget/g1/functions/mass_storage.0
	echo /dev/mmcblk0p1 > /sys/kernel/config/usb_gadget/g1/functions/mass_storage.0/lun.0/file

	mkdir /sys/kernel/config/usb_gadget/g1/functions/ffs.adb
	mkdir /sys/kernel/config/usb_gadget/g1/configs/b.1  -m 0770
	mkdir /sys/kernel/config/usb_gadget/g1/configs/b.1/strings/0x409  -m 0770
	echo 500 > /sys/kernel/config/usb_gadget/g1/configs/b.1/MaxPower
	sleep 1

	ln -s  /sys/kernel/config/usb_gadget/g1/functions/mass_storage.0 /sys/kernel/config/usb_gadget/g1/configs/b.1/f1
	ln -s  /sys/kernel/config/usb_gadget/g1/functions/ffs.adb /sys/kernel/config/usb_gadget/g1/configs/b.1/f2
	echo "ums_adb" > /sys/kernel/config/usb_gadget/g1/configs/b.1/strings/0x409/configuration
	mount -t functionfs adb /dev/usb-ffs/adb

	adbd &
	sleep 1
	echo $UDC > /sys/kernel/config/usb_gadget/g1/UDC

	;;
	stop)
	echo "none" > /sys/kernel/config/usb_gadget/g1/UDC
	;;
	restart|reload)
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
